<template>
  <div class="accountList">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="产品安评报告" name="first">
        <div class="searchBox">
          <el-form :model="cpsearchForm" ref="cpsearchFormRef">
            <el-row>
              <el-form-item prop="createPhone">
                  <div class="item">
                  <span v-html="'创建账号'" class="nameSpan"></span>
                  <el-autocomplete
                    class="inline-input"
                      v-model="cpsearchForm.createPhone"
                    :fetch-suggestions="querySearchPhone"
                    placeholder="请输入创建账号"
                    size="mini"
                      @select="handleSelectcpPhone"
                  ></el-autocomplete>
                </div>
              </el-form-item>
               <el-form-item prop="createName">
                <div class="item">
                  <span v-html="'创建人'" class="nameSpan"></span>
                  <el-autocomplete
                    class="inline-input"
                    v-model="cpsearchForm.createName"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入创建人"
                    @select="handleSelectcpCreateName"
                    size="mini"
                  ></el-autocomplete>
                </div>
              </el-form-item>

              <el-form-item prop="productName">
                <div class="item">
                  <span v-html="'产品名称'" class="nameSpan"></span>
                  <el-input
                    size="mini"
                    v-model="cpsearchForm.productName"
                    placeholder="请输入产品名称"
                  ></el-input>
                </div>
              </el-form-item>
              <el-form-item prop="useType">
                <div class="item">
                  <span v-html="'产品类型'" class="nameSpan"></span>
                  <el-select filterable 
                    clearable
                    size="mini"
                    v-model="cpsearchForm.useType"
                    placeholder="请选择产品类型"
                  >
                    <el-option
                      v-for="item in typeOptions"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item>
                <el-button
                  @click="cpsearchClick"
                  icon="el-icon-search"
                  type="primary"
                  size="mini"
                  >查询</el-button
                >
                <el-button
                  icon="el-icon-refresh"
                  size="mini"
                  @click="cpresetClick"
                  >重置</el-button
                >
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="tableBox" ref="tableBoxRef">
          <el-table
           :height="600"
            v-loading="cptableLoading"
            :header-cell-style="{
              background: '#F8F8F8',
              color: '#333',
              fontSize: '16px',
              fontWeight: '700',
            }"
            border
            :data="cptableData"
            style="width: 100%"
          >
            <el-table-column prop="complete" align="center" label="报告类型">
              <template slot-scope="scope">
                <div>
                  {{ bgTypeFormatter[scope.row.complete] }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="productName"
              align="center"
              label="产品名称"
            >
            </el-table-column>
            <el-table-column prop="useType" align="center" label="使用类型">
              <template slot-scope="scope">
                <div>
                  {{ typeFormatter[scope.row.useType] }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="productType" align="center" label="产品种类">
              <template slot-scope="scope">
                {{scope.row.productType}}
              </template>
            </el-table-column>
            <el-table-column prop="createName" align="center" label="创建人">
            </el-table-column>
            <el-table-column
              prop="createPhone"
              align="center"
              label="创建账号"
            >
            </el-table-column>
            <el-table-column prop="createTime" align="center" label="创建时间">
              <template slot-scope="scope">{{
                scope.row.createTime | formatDate
              }}</template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-download"
                  size="mini"
                  type="text"
                  @click="cpdownloadClick(scope.row)"
               
                  >下载</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <el-card class="footer" v-if="cptotal > 0">
         <div>
            <el-pagination
              background
              @size-change="cphandleSizeChange"
              @current-change="cphandleCurrentChange"
              :current-page="cppageNum"
              :page-sizes="[15, 30, 50, 100]"
              :page-size="cppageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="cptotal"
            >
            </el-pagination>
          </div>
          </el-card>
        </div>
      </el-tab-pane>
      <el-tab-pane label="原料安评报告" name="second">
        <div class="searchBox">
          <el-form :model="searchForm" ref="searchFormRef">
            <el-row>
              <el-form-item prop="createPhone">
                <div class="item">
                  <span v-html="'创建账号'" class="nameSpan"></span>
                  <el-autocomplete
                    class="inline-input"
                    v-model="searchForm.createPhone"
                    :fetch-suggestions="querySearchPhone"
                    placeholder="请输入创建账号"
                     @select="handleSelectPhone"
                    size="mini"
                  ></el-autocomplete>
                </div>
              </el-form-item>
              <el-form-item prop="createName">
                <div class="item">
                  <span v-html="'创建人'" class="nameSpan"></span>
                  <el-autocomplete
                    class="inline-input"
                      v-model="searchForm.createName"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入创建人"
                     @select="handleSelectCreateName"
                    size="mini"
                  ></el-autocomplete>
                </div>
              </el-form-item>
              <el-form-item prop="productName">
                <div class="item">
                  <span v-html="'商品名称'" class="nameSpan"></span>
                  <el-input
                    size="mini"
                    v-model="searchForm.productName"
                    placeholder="请输入商品名称"
                  ></el-input>
                </div>
              </el-form-item>
              <el-form-item prop="ingredientId">
                <div class="item">
                  <span v-html="'中文名称'" class="nameSpan"></span>
                   <el-autocomplete
                    class="inline-input"
                    v-model="zwName"
                    :fetch-suggestions="querySearchName"
                    placeholder="请输入创建人"
                    @select="handleSelectName"
                    size="mini"
                  ></el-autocomplete>
                </div>
              </el-form-item>

              <el-form-item>
                <el-button
                  @click="searchClick"
                  icon="el-icon-search"
                  type="primary"
                  size="mini"
                  >查询</el-button
                >
                <el-button
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetClick"
                  >重置</el-button
                >
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="tableBox" ref="tableBoxRef">
          <el-table
           :height="600"
            v-loading="tableLoading"
            :header-cell-style="{
              background: '#F8F8F8',
              color: '#333',
              fontSize: '16px',
              fontWeight: '700',
            }"
            border
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column prop="productName" align="center" label="商品名称">
            </el-table-column>
            <el-table-column prop="nameCn" align="center" label="中文名称">
            </el-table-column>
            <el-table-column prop="createName" align="center" label="创建人">
            </el-table-column>
            <el-table-column prop="createPhone" align="center" label="创建账号">
            </el-table-column>
            <el-table-column prop="createTime" align="center" label="创建时间">
              <template slot-scope="scope">{{
                scope.row.createTime | formatDate
              }}</template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-download"
                  size="mini"
                  type="text"
                  @click="downloadClick(scope.row)"
                  >下载</el-button
                >
              </template>
            </el-table-column>
          </el-table>

         <el-card class="footer" v-if="total > 0">
         <div>
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[15, 30, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </div>
          </el-card>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
export default {
  data() {
    return {
      zwtableData:[],
      createByList: [],
      createPhoneList: [],
      createNameList:[],
      bgTypeFormatter: {
        2: "完整版",
        1: "简化版",
      },
      typeFormatter: {
        1: "驻留类",
        2: "淋洗类",
      },
      typeOptions: [
        {
          id: 1,
          name: "驻留类",
        },
        {
          id: 2,
          name: "淋洗类",
        },
      ],
      kindOptions: [],
      activeName: "first",
      cpsearchForm: {
        createName: "",
        productName: "",
        useType: "",
        createPhone: "",
      },
      searchForm: {
        productName: "",
        ingredientId: "",
        createPhone: "",
        createName: "",
        createBy:""
      },
      tableLoading: false,
      cptableLoading: false,
      tableHeight: 50,
      tableData: [],
      cptableData: [],
      pageNum: 1,
      cppageNum: 1,
      pageSize: 15,
      cppageSize: 15,
      total: 0,
      cptotal: 0,
      zwName:"",
    };
  },
  methods: {
    handleSelectCreateName(item){
      this.searchForm.createName=item.value
    },
    handleSelectPhone(item){
      this.searchForm.createPhone=item.value
    },
    handleSelectcpCreateName(item){
      this.cpsearchForm.createName=item.value
      
    },
    handleSelectcpPhone(item){
      console.log(item);
      this.cpsearchForm.createPhone=item.value
    },
    /**
     * 获取所有角色列表
     * @param {*}
     */
    async getAllRoleList() {
      await this.$api
        .userListTable({
          size: 200,
          page: 1,
        })
        .then((res) => {
          if (res.data.code == "00000") {
            this.createByList = JSON.parse(
              JSON.stringify(res.data.result.records).replace(/name/g, "value")
            );
            this.createPhoneList = JSON.parse(
              JSON.stringify(res.data.result.records).replace(/phone/g, "value")
            );
          }
        });
    },
    handleSelectName(item){
      this.searchForm.ingredientId=item.id
    },
    querySearch(queryString, cb) {
      var restaurants = this.createByList;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      console.log(results);
      cb(results);
    },
    querySearchPhone(queryString, cb) {
      var restaurants = this.createPhoneList;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      console.log(results);
      cb(results);
    },
    querySearchName(queryString, cb) {
      var restaurants = this.createNameList;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      console.log(results);
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==
          -1
        );
      };
    },
    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getylRawList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getylRawList();
    },
    /**
     * 切换页面大小
     * @param {*} val
     */
    cphandleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.cppageSize = val;
      this.getcpRawList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    cphandleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.cppageNum = val;
      this.getcpRawList();
    },
    handleClick(tab, event) {
      if (tab.name == "second") {
        this.getylRawList();
      } else {
        this.getcpRawList();
      }
    },
    resetClick() {
       this.zwName=''
     
      this.$refs.searchFormRef.resetFields();
      this.pageNum = 1;
      this.getylRawList();
    },
    searchClick() {
      if(this.zwName){
        this.searchForm.ingredientId=this.createNameList.find(item=>item.value==this.zwName)?.id
        if( this.searchForm.ingredientId==undefined){
          this.searchForm.ingredientId='00000'
        }
      }
      this.pageNum = 1;
      this.getylRawList();
    },
    cpresetClick() {
      this.$refs.cpsearchFormRef.resetFields();
      this.cppageNum = 1;
      this.getcpRawList();
    },
    cpsearchClick() {
      this.cppageNum = 1;

      this.getcpRawList();
    },
    async downloadClick(row) {
      const res = await this.$api.download(row.fileName);
      let blob = new Blob([res.data]);
   
      // let filename = decodeURIComponent(res.headers["filename"])
      var link = document.createElement("a");
      var href = window.URL.createObjectURL(blob);
      link.href = href;
      link.download = `${row.fileName}`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(href);

      this.$message.success("请稍等,正在下载");
    },
    async cpdownloadClick(row) {
      const res = await this.$api.download(row.fileName);
      console.log(res);
      let blob = new Blob([res.data]);
      // let filename = decodeURIComponent(res.headers["filename"])
      var link = document.createElement("a");
      var href = window.URL.createObjectURL(blob);
      link.href = href;
      link.download = `${row.fileName}`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(href);

      this.$message.success("请稍等,正在下载");
    },
    async getylRawList() {
      this.tableLoading = true;
      const res = await this.$api.rawList({
        size: this.pageSize,
        page: this.pageNum,
        condition: {
          ...this.searchForm,
          
        },
      });
      this.total = res.data.result.total;
      this.tableData = res.data.result.records;
      this.tableLoading = false;
    },
    async getcpRawList() {
      this.tableLoading = true;
      const res = await this.$api.cpAuditList({
        size: this.cppageSize,
        page: this.cppageNum,
        condition: {
          ...this.cpsearchForm,
        },
      });
      this.cptotal = res.data.result.total;
      this.cptableData = res.data.result.records;
      this.cptableLoading = false;
      this.getExposedList();
    },
    async getExposedList() {
      const res = await this.$api.exposedList({});
      this.kindOptions = res.data.result;
    },
        async getRawList() {
    
      const res = await this.$api.rawmanList({
        size:500,
        page: this.pageNum,
        condition: {
          
        },
      });
   this.createNameList = JSON.parse(
              JSON.stringify(res.data.result.records).replace(/name/g, "value")
            );
     
    },
  },
  created() {
    this.getcpRawList();
    this.getAllRoleList();
    this.getRawList()
  },
};
</script>
<style  lang='less' scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
  .topBtn {
    margin: 10px 0;
    text-align: right;
  }

  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}

</style>
